<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>响应式</title>
</head>

<body>
    <script>
        let bucket = new WeakMap()
        let activeEffect
        const stack = []
        const p = Promise.resolve()
        let isFlushing = false
        const jobQueue = new Set()

        function effect(fn, options = {}) {
            const effectFn = () => {
                cleanup(effectFn)
                activeEffect = effectFn
                stack.push(effectFn)
                fn()
                stack.pop()
                activeEffect = stack[stack.length - 1]
            }
            effectFn.set = []
            effectFn.options = options
            effectFn()
        }

        function cleanup(effectFn) {
            for (let i = 0; i < effectFn.set.length; i++) {
                const set = effectFn.set[i]
                set.delete(effectFn)
            }
            effectFn.set.length = 0
        }

        function flushJob() {
            if (isFlushing) return
            isFlushing = true
            p.then(res => {
                jobQueue.forEach(job => job())
            }).finally(() => {
                isFlushing = false
            })
        }

        function track(target, key) {
            if (!activeEffect) return
            let map = bucket.get(target)
            if (!map) {
                map = new Map()
                bucket.set(target, map)
            }
            let set = map.get(key)
            if (!set) {
                set = new Set()
                map.set(key, set)
            }
            set.add(activeEffect)
            activeEffect.set.push(set)
        }

        function trigger(target, key) {
            const map = bucket.get(target)
            if (!map) return
            const effects = map.get(key)
            const effectsToRun = new Set()
            if (!effects) return
            effects.forEach(fn => {
                if (fn !== activeEffect) effectsToRun.add(fn)
            })
            effectsToRun.forEach(effectFn => {
                if (effectFn.options.scheduler) {
                    effectFn.options.scheduler(effectFn)
                } else {
                    effectFn()
                }
            })
        }

        const data = {
            a: 1
        }

        const obj = new Proxy(data, {
            get(target, key) {
                track(target, key)
                return target[key]
            },
            set(target, key, value) {
                target[key] = value
                trigger(target, key)
            }
        })

        effect(() => {
            console.log(obj.a)
        }, {
            scheduler(fn) {
                // 异步执行
                // setTimeout(fn)
                // 连续自增只输出最终状态,省略中间的过程
                jobQueue.add(fn)
                flushJob()
            }
        })

        obj.a++
        obj.a++
        obj.a++
        console.log('结束了')
    </script>
</body>

</html>